/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;

using Gtk;
using Mono.Unix;

using DCSharp.Xml;

namespace DCSharp.Gui
{
	public class FileNodeStore : TreeStore
	{
		public enum Column {
			Object,
			Name,
			Bytes,
			Size,
			TTH,
			Loaded
		};

		public FileNodeStore() : base(typeof(object),
			typeof(string), typeof(long), typeof(string), typeof(string),
			typeof(bool))
		{
		}

		#region Methods

		public TreeIter AppendDirectory(DirectoryNode directory)
		{
			return AppendDirectory(false, TreeIter.Zero, directory);
		}

		public TreeIter AppendDirectory(TreeIter parent, DirectoryNode directory)
		{
			return AppendDirectory(true, parent, directory);
		}

		public TreeIter AppendFile(FileNode file)
		{
			return AppendValues(file, file.Name, file.Size,
				Util.FormatFileSize(file.Size), file.TTH);
		}

		public TreeIter AppendFile(TreeIter parent, FileNode file)
		{
			return AppendValues(parent, file, file.Name, file.Size,
				Util.FormatFileSize(file.Size), file.TTH);
		}

		public void Load(FileList list)
		{
			if (list == null)
			{
				throw new ArgumentNullException("list");
			}
			Clear();
			foreach (DirectoryNode directory in list.Directories)
			{
				AppendDirectory(directory);
			}
		}

		public void Load(TreeIter iter)
		{
			bool loaded = (bool)GetValue(iter, (int)Column.Loaded);
			if (!loaded)
			{
				PopulateIter(iter);

				TreeIter placeholder;
				if (IterChildren(out placeholder, iter))
				{
					Remove(ref placeholder);
				}
				SetValue(iter, (int)Column.Loaded, true);
			}
		}

		private TreeIter AppendDirectory(bool hasParent, TreeIter parent, DirectoryNode directory)
		{
			int children = directory.Directories.Count +
				directory.Files.Count;

			string size = String.Format(Catalog.GetPluralString("{0} object", "{0} objects",
				children), children);

			TreeIter iter = hasParent
				? AppendValues(parent, directory, directory.Name, (long)children, size)
				: AppendValues(directory, directory.Name, (long)children, size);

			if (children > 0)
			{
				AppendNode(iter); // Add a placeholder
			}
			return iter;
		}

		private void PopulateIter(TreeIter parent)
		{
			DirectoryNode directory = GetValue(parent, (int)Column.Object)
				as DirectoryNode;
			if (directory != null)
			{
				foreach (DirectoryNode childDir in directory.Directories)
				{
					AppendDirectory(parent, childDir);
				}
				foreach (FileNode file in directory.Files)
				{
					AppendFile(parent, file);
				}
			}
		}

		#endregion
	}
}
